package com.bdqn.demo.servlet;

import com.bdqn.demo.pojo.User;
import com.bdqn.demo.service.UserService;
import com.bdqn.demo.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        UserService userService=new UserServiceImpl();
        User user=new User();
        user.setUsername(username);
        user.setPassword(password);
        User user1=userService.login(user);
        if(user1!=null){
            req.getSession().setAttribute("user",user1);
            req.setAttribute("error","登录成功！");
            req.getRequestDispatcher("index.jsp").forward(req,resp);
        }else {
            req.setAttribute("error","登录失败!");
            req.getRequestDispatcher("index.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
